<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户登录</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 400px;
        }

        .login-title {
            text-align: center;
            color: #333;
            margin-bottom: 25px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #555;
            font-weight: bold;
        }

        .form-group input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
            font-size: 16px;
        }

        .form-group input:focus {
            border-color: #4CAF50;
            outline: none;
        }

        .login-btn {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-bottom: 15px;
        }

        .login-btn:hover {
            background-color: #45a049;
        }

        .login-btn:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }

        .register-link {
            text-align: center;
            margin-top: 15px;
        }

        .register-link a {
            color: #4CAF50;
            text-decoration: none;
        }

        .register-link a:hover {
            text-decoration: underline;
        }

        .message {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
            text-align: center;
            display: none;
        }

        .success {
            background-color: #dff0d8;
            color: #3c763d;
            border: 1px solid #d6e9c6;
        }

        .error {
            background-color: #f2dede;
            color: #a94442;
            border: 1px solid #ebccd1;
        }

        .redirect-message {
            margin-top: 15px;
            text-align: center;
            color: #666;
        }
    </style>
</head>
<body>
<div class="login-container">
    <h2 class="login-title">用户登录</h2>
    <form id="loginForm">
        <div class="form-group">
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" required>
        </div>

        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required>
        </div>

        <button type="submit" class="login-btn">登录</button>
    </form>

    <div class="register-link">
        还没有账号？<a href="reg.html" id="registerLink">立即注册</a>
    </div>

    <div id="message" class="message"></div>
    <div id="redirectMessage" class="redirect-message" style="display: none;">
        <p>登录成功！<span id="countdown">3</span>秒后自动跳转到主页...</p>
        <button id="redirectNow" class="login-btn" style="margin-top: 10px;">立即跳转</button>
    </div>
</div>

<script>
    document.getElementById('loginForm').addEventListener('submit', function (e) {
        e.preventDefault();

        // 获取表单数据
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        // 简单验证
        if (!username || !password) {
            showMessage('请输入用户名和密码', 'error');
            return;
        }

        // 禁用提交按钮，防止重复提交
        const submitBtn = document.querySelector('.login-btn');
        submitBtn.disabled = true;
        submitBtn.textContent = '登录中...';

        // 准备发送到后端的数据
        const userData = {
            username: username,
            password: password
        };

        // 发送登录请求
        fetch('/v1/user/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(userData)
        })
            .then(response => response.json())
            .then(data => {
                if (data.state == 2000) { // 假设2000是成功状态码
                    showMessage(data.message);
                    // 显示跳转提示
                    showRedirectMessage();
                    // 这里可以保存用户信息到 sessionStorage 或 localStorage
                    sessionStorage.setItem('currentUser', JSON.stringify(data.data));
                } else if (data.state == 1001) { // 假设1001是用户名或密码错误状态码
                    showMessage(data.message);
                } else {
                    showMessage('登录失败：' + (data.message || '未知错误'), 'error');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('网络错误，请稍后重试', 'error');
            })
            .finally(() => {
                // 恢复提交按钮状态
                submitBtn.disabled = false;
                submitBtn.textContent = '登录';
            });
    });

    function showMessage(text, type) {
        const messageDiv = document.getElementById('message');
        messageDiv.textContent = text;
        messageDiv.className = 'message ' + type;
        messageDiv.style.display = 'block';

        // 3秒后自动隐藏消息
        setTimeout(() => {
            messageDiv.style.display = 'none';
        }, 3000);
    }

    function showRedirectMessage() {
        // 隐藏登录表单
        document.getElementById('loginForm').style.display = 'none';
        document.querySelector('.register-link').style.display = 'none';

        // 显示跳转提示
        document.getElementById('redirectMessage').style.display = 'block';

        // 设置倒计时
        let countdown = 3;
        const countdownElement = document.getElementById('countdown');
        const countdownInterval = setInterval(() => {
            countdown--;
            countdownElement.textContent = countdown;

            if (countdown <= 0) {
                clearInterval(countdownInterval);
                redirectToHome();
            }
        }, 1000);

        // 绑定立即跳转按钮事件
        document.getElementById('redirectNow').addEventListener('click', redirectToHome);
    }

    function redirectToHome() {
        // 跳转到主页，这里假设主页是 index.html
        window.location.href = 'index.html';
    }

    function redirectToRegister() {
        // 跳转到注册页面
        window.location.href = 'reg.html';
    }

    // 为注册链接添加点击事件处理
    document.getElementById('registerLink').addEventListener('click', function (e) {
        e.preventDefault();
        redirectToRegister();
    });
</script>
</body>
</html>
